

package com.zdkj.subway.rca.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;

import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.rca.entity.SwCkSystemCleanView;
import com.zdkj.subway.rca.service.SwCkSystemCleanViewService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * 系统清分数据展示--实际库表
 *
 * @author cooze
 * @date 2020-11-03 21:46:31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swcksystemcleanview")
@Api(value = "swcksystemcleanview", tags = "清分及划账-系统清分数据展示")
public class SwCkSystemCleanViewController {

    private final SwCkSystemCleanViewService swCkSystemCleanViewService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "startDate", value = "开始日期：yyyy-MM-dd", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "endDate", value = "结束日期：yyyy-MM-dd", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "lineCode", value = "线路编码", dataTypeClass = String.class)
            }
    )
    @GetMapping("/page")
    public R getSwCkSystemCleanViewPage(Page page,String lineCode,
                                          String startDate, String endDate) {
        SwCkSystemCleanView swCkSystemCleanView = new SwCkSystemCleanView();
        if(StringUtils.isNotEmpty(lineCode)){
            swCkSystemCleanView.setLineCode(lineCode);
        }
        boolean isDateCondition = StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate);
        return R.ok(swCkSystemCleanViewService.page(page, Wrappers.query(swCkSystemCleanView).orderByDesc("trade_date","-line_code")
                .lambda().between(isDateCondition, SwCkSystemCleanView::getTradeDate, startDate, endDate)));
    }


    /**
     * 通过id查询系统清分数据展示
     *
     * @param lineCode  线路
     * @param tradeDate 日期
     * @return R
     */
    @ApiOperation(value = "浏览", notes = "浏览", hidden = true)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "tradeDate", value = "日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "lineCode", value = "线路编码", dataTypeClass = String.class,required = true)
            }
    )
    @GetMapping("/preview")
    public R getById(@RequestParam("lineCode") String lineCode,
                     @RequestParam("tradeDate") String tradeDate) {
        SwCkSystemCleanView swCkSystemCleanView = new SwCkSystemCleanView();
        swCkSystemCleanView.setLineCode(lineCode);
        swCkSystemCleanView.setTradeDate(LocalDate.parse(tradeDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        return R.ok(swCkSystemCleanViewService.getOne(Wrappers.query(swCkSystemCleanView)));
    }

    /**
     * 导出
     * @param
     * @return
     */
    @ApiOperation(value = "导出", notes = "导出")
    @GetMapping({"/export"})
    @SysLog("系统清分数据展示表-导出")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "startDate", value = "开始日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "endDate", value = "结束日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "lineCode", value = "线路编码", dataTypeClass = String.class)
            }
    )
    public void export(String startDate,String endDate, String lineCode, HttpServletResponse response) {
        if(StringUtils.isEmpty(startDate) ||  StringUtils.isEmpty(endDate)){
            return;
        }
        swCkSystemCleanViewService.export(startDate,endDate,lineCode,response);

    }
}
